package org.example.system.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import jakarta.annotation.Resource;
import org.example.system.model.entity.Vehicle;
import org.example.system.model.query.VehicleDetailQuery;
import org.example.system.model.query.VehiclePageQuery;

import org.example.system.model.query.VehicleQuery;
import org.example.system.model.vo.PageVo;
import org.example.system.model.vo.VehicleListVO;
import org.example.system.model.vo.VehicleVO;
import org.example.system.service.VehicleService;
import org.example.system.mapper.VehicleMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class VehicleServiceImpl extends ServiceImpl<VehicleMapper, Vehicle>
        implements VehicleService{

    @Resource
    private VehicleMapper vehicleMapper;


    @Override
    public boolean removeId(Integer id) {
        return vehicleMapper.removeId(id)>0;
    }

    @Override
    public boolean recoveId(Integer id) {
        return vehicleMapper.recoverId(id)<0;
    }


    /**
     * 根据多条件分页查询车辆列表
     * @param queryParams 查询条件分页对象
     * @return
     */
    @Override
    public PageVo<VehicleVO> findVoPage(VehiclePageQuery queryParams) {
        List<VehicleVO> vehicleList = vehicleMapper.selectVoPage(queryParams);
        Long total = vehicleMapper.selectVoTotalCount(queryParams);
        PageVo<VehicleVO> pageVO = new PageVo<>();
        pageVO.setTotal(total);
        pageVO.setList(vehicleList);
        return pageVO;
    }

    @Override
    public IPage<Vehicle> queryVehicles(VehiclePageQuery query) {
        // 构建分页对象
        Page<Vehicle> page = new Page<>(query.getPageNum(), query.getPageSize());

        // 构建条件查询
        LambdaQueryWrapper<Vehicle> queryWrapper = new LambdaQueryWrapper<>();

        // 如果 brand 不等于默认值 "string"，则添加条件
        if (StringUtils.hasText(query.getBrand())) {
            queryWrapper.like(Vehicle::getBrand, query.getBrand());
        }
        // 如果 modelId 不等于默认值 0，则添加条件
        if (query.getModelId() != null && query.getModelId() != 0) {
            queryWrapper.eq(Vehicle::getModelId, query.getModelId());
        }
        // 如果 licensePlate 不等于默认值 "string"，则添加条件
        if (StringUtils.hasText(query.getLicensePlate())) {
            queryWrapper.like(Vehicle::getLicensePlate, query.getLicensePlate());
        }
        // 如果 shopId 不等于默认值 0，则添加条件
        if (query.getShopId() != null && query.getShopId() != 0) {
            queryWrapper.eq(Vehicle::getLocationId, query.getShopId());
        }
        // 如果 status 不等于默认值 0，则添加条件
        if (query.getStatus() != null && query.getStatus() != 0) {
            queryWrapper.eq(Vehicle::getStatus, query.getStatus());
        }

        // 执行分页查询
        return vehicleMapper.selectPage(page, queryWrapper);
    }


    @Override
    public VehicleDetailQuery CarDetail(Integer id){
        VehicleDetailQuery vehicleDetailList = vehicleMapper.listById(id);

        return vehicleDetailList;
    }


    @Transactional(
            rollbackFor = {Exception.class}
    )
    @Override
    public void updateVehicleState(Integer vehicleId, Integer status) {
        LambdaQueryWrapper<Vehicle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Vehicle::getId, vehicleId);
        Vehicle vehicle = vehicleMapper.selectOne(queryWrapper);
        if(vehicle == null) {
            throw new RuntimeException("此车辆不存在");
        }
        vehicle.setStatus(status);
        vehicleMapper.updateById(vehicle);
    }
}